package cc.customer.web.cusDynamic;

import cc.customer.common.domain.MRecord;
import cc.customer.cusDynamic.domain.ExEmpRecevice;
import cc.customer.common.domain.QueryRequest;
import cc.customer.common.domain.ResponseBo;
import cc.customer.common.utils.Constant;
import cc.customer.common.utils.FileUtils;
import cc.customer.cusDynamic.domain.CusDynamic;
import cc.customer.cusDynamic.services.CusDynamicService;
import cc.customer.system.domain.MyUser;
import cc.customer.system.service.UserService;
import cc.customer.web.sys.controller.base.BaseController;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value="/cusDynamic")
public class CusDynamicController extends BaseController {
private Logger logger = LoggerFactory.getLogger(CusDynamicController.class);

   @Autowired
   private CusDynamicService cusDynamicService;
   @Autowired
   private UserService userService;

   @RequestMapping(value="showReceTrancrDialog")
   public String showReceTrancrDialog(){return "customer/center/saleManage/rece_trance";}

   /**根据当前登录人加载跟进记录*/
   @RequestMapping(value="selectFollowData")
   @ResponseBody
   public Map<String,Object> selectFollowData(QueryRequest request, CusDynamic cus){
      try {
         MyUser user = this.getCurrentUser();
         /**人员查询**/
         String uuidsUser = cus.getDyCreator();
         List<String> selectUser =  new ArrayList<>();
         if (!StringUtils.isEmpty(uuidsUser)){
            List<String> listAll = Arrays.asList(uuidsUser.split(","));
            for (String uuids :listAll){
               if (uuids.contains("jpet")){
                  String deptid = uuids.replace("jpet","");
                  MyUser userAll = new MyUser();
                  userAll.setDeptId(Long.valueOf(deptid));
                  userAll.setStatus("1");
                  List<MyUser> listall =  userService.selectUserByDeptid(userAll);
                  for (MyUser userss:listall){
                     selectUser.add(userss.getUserUuid());
                  }
               }else{
                  selectUser.add(uuids);
               }
            }
         }else{
            selectUser.add(user.getUserUuid());
         }
         cus.setCreatorList(selectUser);
         return super.selectByPageNumSize(request,() -> this.cusDynamicService.selectCusFollow(cus));
      }catch (Exception e){
         logger.error("获取沟通记录失败:"+e.getMessage());
         return ResponseBo.error();
      }
   }

   /**查个人沟通记录当天的所有内容*/
   @RequestMapping(value="getAllRecordData")
   @ResponseBody
   public ResponseBo getAllRecordData(){
      MyUser user = this.getCurrentUser();
      return cusDynamicService.selectAllRecordData(user);
   }
   /**查询沟通记录中的推广资源的领取**/
   @RequestMapping(value="getAllExEmpReceData",method ={RequestMethod.GET} )
   @ResponseBody
   public Map<String,Object> getAllExEmpReceData(CusDynamic dynamic,QueryRequest request){
      MyUser user = this.getCurrentUser();
      List<String> list = user.getAllAuthority();
      if (!list.contains(Constant.AMDIN_AUTORITY)){
         String usersids = dynamic.getDyCreator();
         if (StringUtils.isEmpty(usersids)){
            List<String> listUswers = new ArrayList<>();
            List<MyUser> listAllUser = userService.selectUserByDeptid(user);
            listAllUser.forEach(usesr->{
               listUswers.add(usesr.getUserUuid());
            });
            String jonUser = StringUtils.join(listUswers,",");
            dynamic.setDyCreator(jonUser);
         }
      }
      /**人员查询**/
      String uuidsUser = dynamic.getDyCreator();
      List<String> selectUser =  new ArrayList<>();
      if (!StringUtils.isEmpty(uuidsUser)){
         List<String> listAll = Arrays.asList(uuidsUser.split(","));
         for (String uuids :listAll){
            if (uuids.contains("jpet")){
               String deptid = uuids.replace("jpet","");
               MyUser userAll = new MyUser();
               userAll.setDeptId(Long.valueOf(deptid));
               userAll.setStatus("1");
               List<MyUser> listall =  userService.selectUserByDeptid(userAll);
               for (MyUser userss:listall){
                  selectUser.add(userss.getUserUuid());
               }
            }else{
               selectUser.add(uuids);
            }
         }
      }
      dynamic.setCreatorList(selectUser);
      return this.selectByPageNumSize(request,()->this.cusDynamicService.getExEmpReceData(dynamic));
   }


   /**查询沟通记录中的大资源分配**/
   @RequestMapping(value="getAllbigEmpFemData",method ={RequestMethod.GET} )
   @ResponseBody
   public Map<String,Object> getAllbigEmpFemData(CusDynamic dynamic,QueryRequest request){
      MyUser user = this.getCurrentUser();
      List<String> list = user.getAllAuthority();
      if (!list.contains(Constant.AMDIN_AUTORITY)){
         String usersids = dynamic.getDyCreator();
         if (StringUtils.isEmpty(usersids)){
            List<String> listUswers = new ArrayList<>();
            List<MyUser> listAllUser = userService.selectUserByDeptid(user);
            listAllUser.forEach(usesr->{
               listUswers.add(usesr.getUserUuid());
            });
            String jonUser = StringUtils.join(listUswers,",");
            dynamic.setDyCreator(jonUser);
         }
      }
      /**人员查询**/
      String uuidsUser = dynamic.getDyCreator();
      List<String> selectUser =  new ArrayList<>();
      if (!StringUtils.isEmpty(uuidsUser)){
         List<String> listAll = Arrays.asList(uuidsUser.split(","));
         for (String uuids :listAll){
            if (uuids.contains("jpet")){
               String deptid = uuids.replace("jpet","");
               MyUser userAll = new MyUser();
               userAll.setDeptId(Long.valueOf(deptid));
               userAll.setStatus("1");
               List<MyUser> listall =  userService.selectUserByDeptid(userAll);
               for (MyUser userss:listall){
                  selectUser.add(userss.getUserUuid());
               }
            }else{
               selectUser.add(uuids);
            }
         }
      }
      dynamic.setCreatorList(selectUser);
      return this.selectByPageNumSize(request,()->this.cusDynamicService.getBigEmpReceData(dynamic));
   }


   /**导出推广领取的用户信息**/
   @RequestMapping(value = "/getAllExEmpRereciceByids")
   @ResponseBody
   public ResponseBo getAllExEmpRereciceByids(String ids){
      try {
         MyUser user = this.getCurrentUser();
         List<ExEmpRecevice> listAll = this.cusDynamicService.getExAllExportData(ids,user);
         return  FileUtils.createExcelByPOIKit("领取推广客户资料表", listAll, ExEmpRecevice.class);
      }catch (Exception e){
         e.printStackTrace();
      }
      return ResponseBo.error("获取客户失败");
   }
   /**查询沟通记录中的资源的转让**/
   @RequestMapping(value="getAllCusTransData",method ={RequestMethod.GET} )
   @ResponseBody
   public Map<String,Object> getAllCusTransData(CusDynamic dynamic,QueryRequest request){
      MyUser user = this.getCurrentUser();
      List<String> list = user.getAllAuthority();
      if (!list.contains(Constant.AMDIN_AUTORITY)){
         String usersids = dynamic.getDyCreator();
         if (StringUtils.isEmpty(usersids)){
            List<String> listUswers = new ArrayList<>();
            List<MyUser> listAllUser = userService.selectUserByDeptid(user);
            listAllUser.forEach(usesr->{
               listUswers.add(usesr.getUserUuid());
            });
            String jonUser = StringUtils.join(listUswers,",");
            dynamic.setDyCreator(jonUser);
         }
      }
      /**人员查询**/
      String uuidsUser = dynamic.getDyCreator();
      List<String> selectUser =  new ArrayList<>();
      if (!StringUtils.isEmpty(uuidsUser)){
         List<String> listAll = Arrays.asList(uuidsUser.split(","));
         for (String uuids :listAll){
            if (uuids.contains("jpet")){
               String deptid = uuids.replace("jpet","");
               MyUser userAll = new MyUser();
               userAll.setDeptId(Long.valueOf(deptid));
               userAll.setStatus("1");
               List<MyUser> listall =  userService.selectUserByDeptid(userAll);
               for (MyUser userss:listall){
                  selectUser.add(userss.getUserUuid());
               }
            }else{
               selectUser.add(uuids);
            }
         }
      }
      dynamic.setCreatorList(selectUser);
      return this.selectByPageNumSize(request,()->this.cusDynamicService.getCusTransData(dynamic));
   }
   /**导出推广领取的用户信息**/
   @RequestMapping(value = "/getExAllTranExportData")
   @ResponseBody
   public ResponseBo getExAllTranExportData(String ids){
      try {
         MyUser user  = this.getCurrentUser();
         List<ExEmpRecevice> listAll = this.cusDynamicService.getExAllTranExportData(ids,user);
         return  FileUtils.createExcelByPOIKit("转让客户资料表", listAll, ExEmpRecevice.class);
      }catch (Exception e){
         e.printStackTrace();
      }
      return ResponseBo.error("获取客户失败");
   }


   /**初始化今日沟通记录**/
   @RequestMapping(value = "/getFollowListByType")
   @ResponseBody
   public ResponseBo getFollowListByType(CusDynamic dynamic){
      MyUser user = this.getCurrentUser();

      /**人员查询**/
      String uuidsUser = dynamic.getDyCreator();
      List<String> selectUser =  new ArrayList<>();
      if (!StringUtils.isEmpty(uuidsUser)){
         List<String> listAll = Arrays.asList(uuidsUser.split(","));
         for (String uuids :listAll){
            if (uuids.contains("jpet")){
               String deptid = uuids.replace("jpet","");
               MyUser userAll = new MyUser();
               userAll.setDeptId(Long.valueOf(deptid));
               userAll.setStatus("1");
               List<MyUser> listall =  userService.selectUserByDeptid(userAll);
               for (MyUser userss:listall){
                  selectUser.add(userss.getUserUuid());
               }
            }else{
               selectUser.add(uuids);
            }
         }
      }else{
         selectUser.add(user.getUserUuid());
      }
      dynamic.setCreatorList(selectUser);
      return cusDynamicService.getFollowListByType(dynamic);
   }

}
